"""Setup the portal application"""
import logging
log = logging.getLogger(__name__)

import pylons.test

from portal.config.environment import load_environment
from portal.model.meta import Session, Base
from portal.model import empty_database, User, News
 

def setup_app(command, conf, vars):
    """Place any commands to setup portal here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

    # Create the tables if they don't already exist
    
    if empty_database():
    
        log.info("Creating Database tables")
        Base.metadata.create_all(bind=Session.bind)
        log.info("Finished setting up tables")
        
        log.info('Adding default admin user')
        User().create_default_user_admin()
        News().create_default_news()
        log.info("Finished setting up admin user")
    else :
        raise